/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package barreirasacscomunicator.irc;

import barreirasacscomunicator.BACSComunicator;
import barreirasacscomunicator.Main;
import barreirasacscomunicator.gui.ComunicatorGUI;
import java.applet.AudioClip;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javazoom.jl.decoder.JavaLayerException;
import org.schwering.irc.lib.IRCEventListener;
import org.schwering.irc.lib.IRCModeParser;
import org.schwering.irc.lib.IRCUser;
import javazoom.jl.player.*;


/**
 *
 * @author rjsantos
 */
public class MyIRCEventListener implements IRCEventListener{

     private ComunicatorGUI comunicatorGui;

    /** Creates new form ComunicatorGUI */
    public MyIRCEventListener ( ComunicatorGUI cGui) {

        comunicatorGui = cGui;

    }

    public void onRegistered() {
        System.out.println("onRegistered");
    }

    public void onDisconnected() {
        System.out.println("onDisconnected");
    }

    public void onError(String arg0) {
        System.out.println("onError::" + arg0);
    }

    public void onError(int arg0, String arg1) {
        System.out.println("onError:" + arg0 + " " + arg1);
    }

    public void onInvite(String arg0, IRCUser arg1, String arg2) {
        System.out.println("onInvite");
    }

    public void onJoin(String arg0, IRCUser arg1) {
        System.out.println("onJoin");
        comunicatorGui.getUsers();
        comunicatorGui.notification("["+arg1+"] acabou de entrar");
        //playSound();
        playSound("resources/sounds/ctf/flagscore.mp3");
    }

    public void onKick(String arg0, IRCUser arg1, String arg2, String arg3) {
        System.out.println("onKick");
    }

    public void onMode(String arg0, IRCUser arg1, IRCModeParser arg2) {
        System.out.println("onMode");
    }

    public void onMode(IRCUser arg0, String arg1, String arg2) {
        System.out.println("onMode");
    }

    public void onNick(IRCUser arg0, String arg1) {
        System.out.println("onNick");
    }

    public void onNotice(String arg0, IRCUser arg1, String arg2) {
        System.out.println("onNotice" + arg0 + arg2);
    }

    public void onPart(String arg0, IRCUser arg1, String arg2) {
        System.out.println("onPart");
    }

    public void onPing(String arg0) {
        System.out.println("onPing");
    }

    public void onPrivmsg(String arg0, IRCUser arg1, String arg2) {
        System.out.println("onPrivmsg" + arg0 + " " + arg1.getNick() + " " + arg2);
        comunicatorGui.recieveMessage(arg1.getNick(), arg2);
        playSound("resources/sounds/weapon/sniper.mp3");

    }

    public void onQuit(IRCUser arg0, String arg1) {
        System.out.println("onQuit");
    }

    public void onReply(int arg0, String arg1, String arg2) {
        System.out.println( "onReply arg0 "+arg0+ " arg1 "+arg1+ " arg2 "+arg2);

        if( arg0==353){

            comunicatorGui.setUsers(arg2);
        }
    }

    public void onTopic(String arg0, IRCUser arg1, String arg2) {
        System.out.println("onTopic arg0 "+arg0+ " arg2 "+arg2);
    }

    public void unknown(String arg0, String arg1, String arg2, String arg3) {
        System.out.println( "unknown arg0 "+ arg0 + " arg1 "+arg1+ " arg2 "+arg2+ " arg3 "+arg3);
    }

     private void playSound(){
        //URL url = getClass().getResource("/resources/sounds/ctf/flagscore.wav");
        //AudioClip clip = java.applet.Applet.newAudioClip(url);
        //clip.play();

        playSound("resources/sounds/ctf/flagscore.mp3");

    }

    public void playSound(String filename) {

        Player player;
        InputStream is;

        try
        {
            // Create an InputStream to the file
            is = new FileInputStream( filename );
            player = new Player(is);
            player.play();
            
        } catch (JavaLayerException ex) {
            ex.printStackTrace();
            //Logger.getLogger(MyIRCEventListener.class.getName()).log(Level.SEVERE, null, ex);
        }catch( Exception e ){
            e.printStackTrace();
        }




 
   }


}
